﻿using System;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using Windows.Devices.Geolocation;
using HappyTravelling.DataModel;
using HappyTravelling.GeocodeService;
using HappyTravelling.Resources;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Shell;
using Telerik.Windows.Controls;
using System.Device.Location;
using Polygon = System.Windows.Shapes.Polygon;
using Rectangle = System.Windows.Shapes.Rectangle;


using HappyTravelling.ThanhService;

namespace HappyTravelling.Views
{
    public partial class AddAccount : PhoneApplicationPage
    {

        private Geolocator geolocator = null;
        private bool tracking = false;
        private ProgressIndicator pi;
        private MapLayer PushpinMapLayer;
        private bool isMenuOpen = false;
        private Point menuPos;

        private GeocodeServiceClient _svc;
        private ApplicationBar addBar, nextBar;
        private ApplicationBarIconButton bt1, bt2, bt3, bt4;
        private UserModel NewUser;

        public AddAccount()
        {
            InitializeComponent();
            CreateApplicationBarButtons();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            NewUser = new UserModel();
            base.OnNavigatedTo(e);
        }
        private void CreateApplicationBarButtons()
        {
            addBar = new ApplicationBar();
            bt1 = new ApplicationBarIconButton
            {
                Text = AppResources.done,
                IconUri = new Uri("/Assets/AppBar/appbar.check.rest.png", UriKind.Relative)
            };
            bt1.Click += new EventHandler(btnAdd_Click);
            bt1.IsEnabled = true;
            addBar.Buttons.Add(bt1);

            bt2 = new ApplicationBarIconButton
            {
                Text = AppResources.cancel,
                IconUri = new Uri("/Assets/AppBar/appbar.cancel.rest.png", UriKind.Relative)
            };
            bt2.Click += new EventHandler(btnCancel_Click);
            addBar.Buttons.Add(bt2);

            nextBar = new ApplicationBar();
            nextBar.Opacity = 0.5;
            bt3 = new ApplicationBarIconButton
            {
                Text = AppResources.done,
                IconUri = new Uri("/Assets/AppBar/next.png", UriKind.Relative)
            };
            bt3.Click += new EventHandler(btnNext_Click);
            nextBar.Buttons.Add(bt3);

            bt4 = new ApplicationBarIconButton
            {
                Text = AppResources.cancel,
                IconUri = new Uri("/Assets/AppBar/appbar.cancel.rest.png", UriKind.Relative)
            };
            bt4.Click += new EventHandler(btnCancel_Click);
            nextBar.Buttons.Add(bt4);

            ApplicationBar = nextBar;
            ApplicationBar.Opacity = 0.5;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //Tao su kien va day len service
            try
            {
                WebService1SoapClient client = new WebService1SoapClient();
                client.CreateUserAsync(tbName.Text, tbPass.Password, tbName.Text);  
                client.CreateUserCompleted += client_CreateUserCompleted;
                NavigationService.GoBack();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        void client_CreateUserCompleted(object sender, CreateUserCompletedEventArgs e)
        {
            int addUserReturnCode = (int)e.Result;
            if (addUserReturnCode == 1)
            {
                // Add thành công
                MessageBox.Show("Bạn đã tạo thành công tài khoản trên Dating!");
            }
            else
            {
                // Chắc là nghẻo.
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int index = MainPivot.SelectedIndex + 1;
            if (index < MainPivot.Items.Count)
                MainPivot.SelectedIndex = index;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void MainPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.Focus();
            tabControl1.PivotSelectedIndex = MainPivot.SelectedIndex;

            if (MainPivot.SelectedIndex == MainPivot.Items.Count - 1)
                ApplicationBar = addBar;
            else
                ApplicationBar = nextBar;
            if (MainPivot.SelectedIndex == 0)
                tbName.Focus();
            else if (MainPivot.SelectedIndex == 1)
            {
                //App.CurrentUser.Username = tbName.Text;
            }
            ApplicationBar.Opacity = 0.5;
        }
    }
}